package dbzx.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.junit.Test;

import model.Emp;

public class IOTest {
	/*
	 * File类支持的4种方式：
	 * String path =  src/main/resources/byte.data;
	 * String path =  "byte.data";
	 * String localPath = "E:\\demo\\pdfCreat\\byte.data";
	 * String localPath = "E:/demo/pdfCreat/byte.data";
	 */
	private static String dirPath = "src/main/resources/file/";
	
	/**
	 * FileInputStream和FileOutputStream配合以单字节方式读写文件中内容
	 * @throws IOException
	 */
	@Test
	public void testFileInputOutputStream() throws IOException{
		String path = dirPath + "byte.data";
		String copyPath = dirPath + "copyByte.data";
		
		File file = new File(path);
		File copyFile = new File(copyPath);
		
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(copyFile);
		
		int read = -1;
		while((read = fis.read()) != -1){
			System.out.print((char)read);
			fos.write(read);
		}
		
		fis.close();
		fos.close();
	}
	
	/**
	 * FileInputStream和FileOutputStream以字节数组方式读写文件内容
	 * @throws IOException
	 */
	@Test
	public void testFileInputOutputSteamBytes() throws IOException{
		String path =  dirPath + "byte.data";
		String copyPath = dirPath + "copyByte.data";
		
		File file = new File(path);
		File copyFile = new File(copyPath);
		
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(copyFile);
		
		int length = -1;
		byte[] bytes = new byte[1024*10];
		while((length = fis.read(bytes)) != -1){
			System.out.println(new String(bytes,0,length));
			fos.write(bytes, 0, length);
		}
		fis.close();
		fos.close();
	}
	
	/**
	 * BufferedInputStream和BufferedOutputStream是缓冲类字节流，用于提高数据的读写效率。
	 * 用法与FileInputStream和FileOutputstream相同
	 * @throws IOException
	 */
	@Test
	public void testCopy() throws IOException{
		String path =  dirPath + "byte.data";
		String copyPath = dirPath + "copyByte.data";
		
		FileInputStream fis = new FileInputStream(path);
		BufferedInputStream bis = new BufferedInputStream(fis);
		
		FileOutputStream fos = new FileOutputStream(copyPath);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		int length = -1;
		//1024*10个字节是效率最高的方式
		byte[] data = new byte[1024*10];
		while((length = bis.read(data)) != -1){
			bos.write(data, 0, length);
		}
		//刷新缓存流，一般最后一次读取的数据没有达到缓存的上限，可能会没有写出
		bos.flush();
		bis.close();
		bos.close();
		
	}
	
	
	/**
	 * ObjectInputStream和ObjectOutputStream从文件中读写对象
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@Test
	public void testObjectReadAndWrite() throws IOException, ClassNotFoundException{
		String path =  dirPath + "object.data";
		File file = new File(path);
		//写入
		Emp emp = new Emp(1,"wick","coder","男",System.currentTimeMillis());
		Emp emp2 = new Emp(2,"wick","coder","男",System.currentTimeMillis());
		FileOutputStream fos = new FileOutputStream(file);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(emp);
		oos.writeObject(emp2);
		
		//读取
		FileInputStream fis = new FileInputStream(file);
		ObjectInputStream ois = new ObjectInputStream(fis);
		while(true) {
			
			try {
				Emp readEmp = (Emp) ois.readObject();
				System.out.println(readEmp);
			}catch(EOFException e) {
				break;
			}
		}
		
		
		
		
		ois.close();
		oos.close();
	}
	
	@Test
	public void testDataInputOutputStreamAndDataOutputStream() throws IOException {
		String path = dirPath + "data.data";
		File file = new File(path);
		FileOutputStream fos = new FileOutputStream(file);
		DataOutputStream dos = new DataOutputStream(fos);
		dos.writeDouble(new Double(3.55));
		dos.writeDouble(new Double(4.44));
		dos.close();
		
		FileInputStream fis = new FileInputStream(file);
		DataInputStream dis = new DataInputStream(fis);
		while(dis.available()>0) {
			System.out.println(dis.readDouble());
		}
		dis.close();
		
	}
	
	
	
	
	
	
	
	@Test
	public void testInputStreamReader() throws IOException{
		File file = new File("src/hello.data");
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis,"utf-8");
		int c = -1;
		while((c =(isr.read())) != -1){
			System.out.print((char)c);
		}
		isr.close();
	}
	
	@Test
	public void testOutputStreamWriter() throws IOException{
		File file = new File("src/write.data");
		FileOutputStream fos = new FileOutputStream(file);
		OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
		osw.write("你好啊！");
		osw.close();
	}
	
	@Test
	public void testBufferedReader() throws IOException{
		File file = new File("src/hello.data");
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis, "utf-8");
		BufferedReader br = new BufferedReader(isr);
		String data = null;
		
		while((data = br.readLine()) != null){
			System.out.println(data);
		}
		br.close();
	}
	
	@Test
	public void testPrintWriter() throws IOException{
		File file = new File("src/write.data");
		PrintWriter pw = new PrintWriter(file);
		pw.write("哈哈哈哈！");
		pw.close();
	}
	
	
}
